<?php
require_once("inc.member.php");

$this_title="$vars[member_title] &raquo; ".__("Downline Stockist List");
$content_title = $page_title=__("Downline Stockist List");

require_once("inc.stockist_tab.php");

$add_edit_file=$vars["file"]["member"]["edit_stockist_code"];

$item_per_page=100;

//#####LIST MEMBER STOCKIST#####
$dist=mysql_fetch_assoc(mysql_query("select ref_left, ref_right from $db->users where id='$uid'"));
$left=$dist['ref_left'];
$right=$dist['ref_right'];
$count_sql="select count(*) from $db->users where ref_left between $left and $right and is_stockist='y' and stockist_uid=0";
$record_sql="select * from $db->users where ref_left between $left and $right and is_stockist='y' and stockist_uid=0 order by cdate,id limit <%limit_start%>, <%limit%>";

$cur_page=$_GET["nav"];
$total_record=@mysql_result(mysql_query($count_sql), 0);
$total_page=ceil($total_record/$item_per_page);
$page=($cur_page? ($cur_page>$total_page? $total_page : ($cur_page<1? 1 : $cur_page)) : 1);
$limit_start=($page-1)*$item_per_page;
$nav_link=format_page($page, $total_page, 20);
$this_page_total=@mysql_num_rows($r=mysql_query(str_replace("<%limit_start%>", $limit_start, str_replace("<%limit%>", $item_per_page, $record_sql))));
$k=0;

for($i=0;$i<$this_page_total;$i++){
	$run_no = (($page - 1) * $item_per_page) + $i + 1;
	$stockist=mysql_fetch_assoc($r);
	$edit_link="<a href=\"$add_edit_file?type=edit&id=".$stockist["id"]."\"><img src='".M_URL."/images/icons/edit.png'></a>";
	$sale_row.="
	<tr class='row$k'>
		<td>$run_no</td>
		<td>$edit_link</td>
		<td>".$stockist["id"]."</td>
		<td>".$stockist["name"]."</td>
		<td>".$stockist["username"]."</td>
		<td>".$stockist["stockist_uid"]."</td>
		<td>".$stockist["stockist_code"]."</td>
		<td>".date($vars["date_format"], strtotime($stockist['cdate']))."</td>
	</tr>";
	$k=1-$k;
}


//listing
$sales_list=($errmsg || $msg? $errmsg.$msg : "").
"<p class='center'></p>
$search_ui<br />\n
$listing_text $nav_link
<table class='pbl_table'>
	<tr class='pbl_header'>		
		<td>".__("No")."</td>
		<td width='18'><img src='".M_URL."/images/icons/edit.png'></td>
		<td>".__("ID")."</td>
		<td>".__("Name")."</td>
		<td>".__("Username")."</td>
		<td>".__("Selected By Member ID")."</td>
		<td>".__("Stockist Code")."</td>
		<td>".__("Created Date")."</td>
	</tr>
	$sale_row
</table>
</form>";
//#####END MEMBER STOCKIST LIST#####

$content="<h2>$page_title</h2>".$sales_list;
$content=str_replace("<%tab_content%>", $content, $tab);

print format_member_page($content, $this_title, $content_title, $css);

?>